﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using McMaster.Extensions.CommandLineUtils;

namespace compose_docx
{
    class Program
    {
        public static int Main(string[] args)
            => CommandLineApplication.Execute<Program>(args);

        [Argument(0, Description = "Source files")]
        public string[] SourceFiles { get; }

        [Option(ShortName = "o")]
        public string OutputFile { get; }

        public void OnExecute(CommandLineApplication app)
        {
            if (string.IsNullOrEmpty(OutputFile) || SourceFiles == null || SourceFiles.Length == 0)
            {
                app.ShowHelp();
                return;
            }
            Compose(SourceFiles, OutputFile);
        }


        private string Compose(string[] sourceFiles, string outputFile)
        {
            string altChunkIdBase = "acID";
            int altChunkCounter = 1;
            string altChunkId = altChunkIdBase + altChunkCounter.ToString();

            // 选择第一个文件为主文件，保留里面的样式
            string masterFile = sourceFiles[0];
            File.Copy(masterFile, outputFile, true);

            // 后续文件合并进来
            List<string> fileList = sourceFiles.ToList();
            fileList.RemoveAt(0);

            // 打开目标文件进行合并
            MainDocumentPart wdDocTargetMainPart = null;
            Document docTarget = null;
            AlternativeFormatImportPartType afType;
            using (WordprocessingDocument wdPkgTarget = WordprocessingDocument.Open(outputFile, true))
            {
                wdDocTargetMainPart = wdPkgTarget.MainDocumentPart;

                docTarget = wdDocTargetMainPart.Document;
                Paragraph lastParaTarget = docTarget.Body.Descendants<Paragraph>().Last();
                ParagraphProperties paraPropTarget = lastParaTarget.ParagraphProperties;
                if (paraPropTarget == null)
                {
                    paraPropTarget = new ParagraphProperties();
                }
                Run paraRun = lastParaTarget.Descendants<Run>().FirstOrDefault();

                foreach (string fi in fileList)
                {

                    afType = AlternativeFormatImportPartType.WordprocessingML;
                    AlternativeFormatImportPart chunk = wdDocTargetMainPart.AddAlternativeFormatImportPart(afType, altChunkId);
                    System.IO.FileStream fsSourceDocument = new FileStream(fi, FileMode.Open);
                    chunk.FeedData(fsSourceDocument);
                    //Create the chunk
                    AltChunk ac = new AltChunk
                    {
                        //Link it to the part
                        Id = altChunkId
                    };
                    docTarget.Body.Append(ac);
                    docTarget.Save();
                    altChunkCounter += 1;
                    altChunkId = altChunkIdBase + altChunkCounter.ToString();
                    chunk = null;
                    ac = null;
                }
            }

            return outputFile;
        }


    }
}


